package com.example.demo;


import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

class student{
    int id;

    String name;

    public student() {}

    public student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}

@RequestMapping("/request")
@Controller
@ResponseBody
public class RequestController {

    @RequestMapping("/r1")
    public String r1(String keyword){
        return "接收参数:" + keyword;
    }

    @RequestMapping("r2")
    public String r2(String name, int id){
        return "receive:" + name + id;
    }

    @RequestMapping("r3")
    public String r3(Integer number){
        return "receive" + number;
    }

    @RequestMapping("r4")
    public String r4(student s1){
        return "receive" + s1.toString();
    }

    //从前端接受参数n,赋值给name
    @RequestMapping("/r5")
    public String r5(@RequestParam(value = "n", required = false)String name){
        return "receive" + name;
    }

    //传递数组
    @RequestMapping("/r6")
    public String r6(String[] array){
        return "receive" + array.toString();
    }

    //传递集合
    @RequestMapping("/r7")
    public String r7(@RequestParam List<Integer> list){
        return "receive:" + list;
    }

    //传递json
    @RequestMapping("/r8")
    public String r8(@RequestBody student student1){
        return student1.toString();
    }

    //从URL中获取参数
    @RequestMapping("/path/{pathid}")
    public String r9(@PathVariable Integer pathid){
        return "获取路径id" + pathid;
    }

    //上传文件
    @RequestMapping("/r10")
    public String r10(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\桌面\\" + file.getOriginalFilename()));
        return "文件上传成功";
    }

    //返回当前网页所有的cookie
    @RequestMapping("/r11")
    public String r11(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for(Cookie cookie:cookies){
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "返回所有cookie成功";
    }

    //返回cookie中单个键的值
    @RequestMapping("/r12")
    public String r12(@CookieValue ("name") String name){
        return "cookie该key的值" + name;
    }

    //存储session
    @RequestMapping("/r13")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();

        session.setAttribute("name","jerry");
        session.setAttribute("age",20);

        return "session设置成功";
    }

    //获取session
    @RequestMapping("/r14")
    public String getSession1(HttpServletRequest request){
        HttpSession session = request.getSession(false);

        if(session == null){
            return "用户未登录";
        }else{
            String name = (String) session.getAttribute("name");
            return "登录用户为:" + name;
        }
    }

    @RequestMapping("/r15")
    public String getSession2(HttpSession session){
        String name = (String) session.getAttribute("name");
        return "登录用户为: " + name;
    }

    @RequestMapping("/r16")
    public String getSession3(@SessionAttribute("name") String name){
        return "登陆用户为: " + name;
    }






}
